package cn.HL.basic.Interceptor;

import cn.HL.basic.anno.PreAuthorize;
import cn.HL.basic.jwt.JwtUtils;
import cn.HL.basic.jwt.LoginResult;
import cn.HL.basic.jwt.Payload;
import cn.HL.basic.jwt.RsaUtils;
import cn.HL.system.mapper.PermissionMapper;
import cn.HL.user.domain.Logininfo;
import cn.HL.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
   /* @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PermissionMapper permissionMapper;*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头里面的
        String token = request.getHeader("U-TOKEN");
        //设置编码集 和输出转为json
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=UTF-8");


        if(StringUtils.hasLength(token)){
            //有token
          /*  Object objTem = redisTemplate.opsForValue().get(token);*/
            //现在我们就不从redis里面去拿 我们即系就可以了
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
            Payload<LoginResult> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginResult.class);
            if(payload!=null){
                //判断是否是员工
              Logininfo logininfo = payload.getUserInfo().getLogininfo();
                if(logininfo.getType()==0) {
                    //获取请求的sn
                    //。得到当前请求的资源 url
                    HandlerMethod method = (HandlerMethod) handler;
                    PreAuthorize annotation = method.getMethodAnnotation(PreAuthorize.class);
                    //我们只需要比较sn即可
                    if (annotation != null) {
                        String sn = annotation.value();
                        //查询这个员工是否拥有这个权限
                       /* List<String> permissions = permissionMapper.loadPermissionsByLogininfoId(logininfo.getId());*/
                        List<String> permissions = payload.getUserInfo().getPermissions();
                        //不包含这个权限我们就提示
                        if (!permissions.contains(sn)) {

                            PrintWriter writer = response.getWriter();
                            //写出
                            writer.write("{\"success\":false,\"message\":\"noPermission\"}");
                            //刷新
                            writer.flush();
                            //关闭
                            writer.close();
                            return false;
                        }
                        //包含就放行
                    }
                }
                //说明没有过期  刷新时间
               /* redisTemplate.opsForValue().set(token,objTem,30, TimeUnit.MINUTES);*/
                return true;
            }
        }
        PrintWriter writer = response.getWriter();
        //写出
        writer.write("{\"success\":false,\"message\":\"noLogin\"}");
        //刷新
        writer.flush();
        //关闭
        writer.close();
        return false;
    }

}
